import { getDetailsByIdApi } from "@/api/client/documentManagement/waybillManagement";
import { getChargeCostByWabbillApi, getDetailsApi } from "@/api/waybillManagement/businessWaybill/waybillQuotation";
import FormHook from "@/hook/formHook";
import { reactive, ref } from "vue";

export default function InitHook() {
  const formState = ref<any>({
    bizPreOrderShipmentResDto: {},
    bizOrderShipmentQuotation: {},
    bizOrderShipmentCosts: [],
    freightCost: {
      opType: "2",
      adjustUnitPrice: 0,
      unitPrice: 0,
      clearingUnitPrice: 0,
      adjustReason: ""
    },
    totalFreightPrice: 0,
    totalFreightPriceStr: "",
    totalAdjustPrice: 0,
    totalAdjustPriceStr: "",
    totalAppendCostPrice: 0,
    totalAppendCostPriceStr: "",
    totalPrice: 0,
    totalPriceStr: ""
  });
  const { resetFields } = FormHook({ formState });
  const offerStatus = ref<number>(1); //提交报价状态， 1未提交 2已提交
  const loading = ref<boolean>(false);
  const dialogOpts = reactive<Base.DialogOpts>({
    title: "修改送货仓库",
    width: "720px",
    type: 1,
    passData: {},
    component: "",
    visible: false
  });
  const drawerOpts = reactive<Base.DrawerOpts>({
    title: "修改送货仓库",
    width: "720px",
    type: 1,
    passData: {},
    component: "",
    visible: false
  });
  //存下调整单价的值
  const getAdjustUnitPrice = ref<number>(0);
  //存下加或减
  const saveOpType = ref<string>("2");

  //判断是否第一次进入详情页
  const firstGetInfoBoolean = ref<boolean>(true);

  // 没有报价ID
  const init = (id: string) => {
    loading.value = true;

    return getDetailsByIdApi(id)
      .then((res: any) => {
        const { code, data } = res;
        if (code === 200) {
          Object.assign(formState.value, data);
          // 运单详情信息
          formState.value.bizPreOrderShipmentResDto = data;
          // 基础报价信息  opType： 1 新增  2减少
          // if (formState.value.freightCost.adjustUnitPrice > 0) {
          //   formState.value.freightCost.opType = "1";
          // } else {
          //   formState.value.freightCost.opType = "2";
          // }

          if (formState.value.freightCost.adjustUnitPrice && formState.value.freightCost.adjustUnitPrice > 0 && firstGetInfoBoolean.value) {
            formState.value.freightCost.opType = "1";
          }

          if (formState.value.freightCost.adjustUnitPrice === 0 && firstGetInfoBoolean.value) {
            formState.value.freightCost.opType = "2";
          }

          if (formState.value.freightCost.adjustUnitPrice < 0 && firstGetInfoBoolean.value) {
            formState.value.freightCost.opType = "2";
          }

          //第一次进入页面后变为false
          firstGetInfoBoolean.value = false;
          getChargeCostByWabbillApi({ type: 2, waybillNums: [data.waybillNum] }).then((res: any) => {
            const { code, data } = res;
            if (code === 200) {
              formState.value.bizOrderShipmentCosts = data.map((t: any) => ({ ...t, mergeUnitPrice: t.mergeUnitPrice ? true : false })) || [];
            }
          });
        }
      })
      .finally(() => {
        loading.value = false;
      });
  };
  // 有报价ID
  const initInfo = (id: string) => {
    loading.value = true;
    return getDetailsApi({ id })
      .then((res: any) => {
        const { code, data } = res;
        if (code === 200) {
          formState.value = data;
          formState.value.bizOrderShipmentCosts = data.bizOrderShipmentCosts.map((t: any) => ({ ...t, mergeUnitPrice: t.mergeUnitPrice ? true : false })) || [];

          formState.value.freightCost = data.freightCost;
          // 基础报价信息  opType： 1 新增  2减少

          // if (formState.value.freightCost.adjustUnitPrice > 0) {
          //   formState.value.freightCost.opType = "1";
          // } else {
          //   formState.value.freightCost.opType = "2";
          // }

          if (formState.value.freightCost.adjustUnitPrice && formState.value.freightCost.adjustUnitPrice > 0 && firstGetInfoBoolean.value) {
            formState.value.freightCost.opType = "1";
          }

          if (formState.value.freightCost.adjustUnitPrice === 0 && firstGetInfoBoolean.value) {
            formState.value.freightCost.opType = "2";
          }

          if (formState.value.freightCost.adjustUnitPrice < 0 && firstGetInfoBoolean.value) {
            formState.value.freightCost.opType = "2";
          }

          //第一次进入页面后变为false
          firstGetInfoBoolean.value = false;
        }
      })
      .finally(() => {
        loading.value = false;
      });
  };
  return {
    formState,
    loading,
    dialogOpts,
    drawerOpts,
    init,
    initInfo,
    resetFields,
    getAdjustUnitPrice,
    offerStatus,
    saveOpType,
    firstGetInfoBoolean
  };
}
